//
//  HomepageNoticeView.swift
//  MM2
//
//  Created by 李成明 on 2022/1/15.
//

import UIKit

class HomepageNoticeView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(bgView)
        addSubview(scrollView)
        addSubview(noticeImgView)
        
        scrollView.addSubview(bgImageView)
        scrollView.addSubview(contentLabel)
        
        bgImageView.frame = scrollView.bounds
        
        
        noticeImgView.snp.makeConstraints { make in
            make.bottom.equalTo(scrollView.snp.top)
            make.centerX.equalTo(self)
            make.size.equalTo(CGSize(width: 70, height: 70))
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var contentW = (kScreenWidth/3)*2
    var contentH = kScreenHeight/2
    var valueStr: String = "" {
        didSet {
            contentLabel.text = valueStr
            let size = contentLabel.sizeThatFits(CGSize(width: contentW, height: CGFloat.greatestFiniteMagnitude))
            contentLabel.frame = CGRect(x: 5, y: 5, width: contentW, height: size.height)
            scrollView.contentSize = size
        }
    }
    
    private lazy var bgView: UIView = {
        let view = UIView(frame: bounds)
        view.backgroundColor = .black
        view.alpha = 0.3
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(bgViewCickAction))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    private lazy var scrollView: UIScrollView = {
        let view = UIScrollView(frame: CGRect(x: kScreenWidth/6 - 5, y: 220, width: contentW + 10, height: contentH + 10))
        view.bounces = false
        view.backgroundColor = .white
        return view
    }()
    
    private lazy var noticeImgView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "front_notice_icon"))
//        view.backgroundColor = .white
        return view
    }()
    
    private lazy var bgImageView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "rank_bottom_bg"))
        return view
    }()
    
    private lazy var contentLabel: UILabel = {
        let view = UILabel()
        view.font = .systemFont(ofSize: 24)
        view.numberOfLines = 0
        return view
    }()
    
    @objc private func bgViewCickAction() {
        self.removeFromSuperview()
    }
}
